package com.xing.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.reggie.common.CustomException;
import com.xing.reggie.entity.Category;
import com.xing.reggie.entity.Dish;
import com.xing.reggie.entity.Setmeal;
import com.xing.reggie.mapper.CategoryMapper;
import com.xing.reggie.service.CategoryService;
import com.xing.reggie.service.DishService;
import com.xing.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService{
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 通过Id删除分类，删除前需要判断
     * @param id
     */
   public void remove(Long id){
       LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
       //添加查询条件，根据ID进行查询
       dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
       int count1 = dishService.count(dishLambdaQueryWrapper);

       //查询当前分类是否关联了菜品，如果有，抛出异常
        if (count1>0){
            //抛出异常
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }
       //查询当前分类是否关联了套餐，如果有，抛出异常
       LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
       //添加查询条件，根据ID进行查询
       setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
       int count2 = setmealService.count(setmealLambdaQueryWrapper);

       //查询当前分类是否关联了菜品，如果有，抛出异常
       if (count2 >0){
           //抛出异常
           throw new CustomException("当前分类下关联了套餐，不能删除");

       }

       //可以正常删除
       super.removeById(id);

   }
}
